/*
@author: Shizuku
@file: 2022-12-07-practice.go
@time: 2022/12/7 16:05
*/
package main

import (
	"fmt"
	"math/rand"
	"time"
)

type treeNode struct {
	leftNode *treeNode
	data int
	rightNode *treeNode
}

func insert(t * treeNode, v int) *treeNode{
	if t == nil{
		return &treeNode{nil, v, nil}
	}
	if v == t.data{
		return t
	}
	if v < t.data{
		t.leftNode = insert(t.leftNode, v)
		return t
	}
	t.rightNode = insert(t.rightNode, v)
	return t
}

func createTree(n int) *treeNode {
	var t *treeNode
	rand.Seed(time.Now().Unix())
	for i :=0; i < 2 * n ; i++{
		temp := rand.Intn(2 * n)
		t = insert(t, temp)
	}
	return t
}

func traverse(t *treeNode) {
	if t == nil {
		return
	}
	traverse(t.leftNode)
	fmt.Print(t.data, " ")
	traverse(t.rightNode)
}

func main() {
	tree := createTree(10)
	fmt.Println("根节点的值为：", tree.data)
	traverse(tree)
	fmt.Println()
	tree = insert(tree,-10)
	traverse(tree)
}